package co.rcantor.dev.siasar.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Environment;
import co.rcantor.dev.siasar.R;
import co.rcantor.dev.siasar.SIASARActivity;

public class ActualizarBD extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.updatebd);
		
		String FILENAME = "SIASAR-UPDATE.update";
    	String string = "";	    	

    	FileInputStream fis = null;
    	
		try {		    
			File file = new File(Environment.getExternalStorageDirectory()+"/"+FILENAME);
			if(file.exists()){
				fis = new FileInputStream(file);
				byte bt[] = new byte[(int)file.length()];
				int numBytes = fis.read(bt);
				string = new String(bt);
				String[] update = string.split("\n");
				
				int version = Integer.valueOf(update[0].substring(0,1));
				
				DBManager dbm = new DBManager(this,"SIASARDB", null, 1);
				SQLiteDatabase db = dbm.getWritableDatabase();
				dbm.setUpdate(update);
				ProgressDialog progressDialog;
				progressDialog = new ProgressDialog(ActualizarBD.this);
				    if(db!=null){												
						progressDialog.setMessage("Actualizando...");
						progressDialog.setCancelable(false);
						progressDialog.show();						
						dbm.onUpgrade(db, (version-1), version);
				    }
				    
			        db.close();
			        progressDialog.hide();
	
				System.out.println(numBytes);
				
				AlertDialog.Builder builder = new AlertDialog.Builder(ActualizarBD.this);
		    	builder.setMessage("BASE DE DATOS ACTUALIZADA EXITOSAMENTE.")
		    	       .setPositiveButton("OK", new DialogInterface.OnClickListener() {
		    	           public void onClick(DialogInterface dialog, int id) {
		    	        	finish();
		    	   		    Intent intent = new Intent(ActualizarBD.this,SIASARActivity.class);
		    	   		    startActivity(intent);
		    	           }
		    	       });
		    	AlertDialog alert = builder.create();
		    	alert.show();
				
			}else{
				AlertDialog.Builder builder = new AlertDialog.Builder(ActualizarBD.this);
		    	builder.setMessage("NO EXISTE EL ARCHIVO DE ACTUALIZACION O NO HAY ACTUALIZACIONES PENDIENTES.")
		    	       .setPositiveButton("OK", new DialogInterface.OnClickListener() {
		    	           public void onClick(DialogInterface dialog, int id) {
		    	        	finish();
		    	   		    Intent intent = new Intent(ActualizarBD.this,SIASARActivity.class);
		    	   		    startActivity(intent);
		    	           }
		    	       });
		    	AlertDialog alert = builder.create();
		    	alert.show();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
}
